package com.yfbao.horizon.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.google.common.collect.Lists;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ZipLogAnalysis {

    //锁异常： IllegalMonitorStateException
    private static String outFile = "outRes.log";

    public static void main(String[] args) {
//        String path ="C:\\Users\\byf78\\Desktop\\日志分析\\客户问题\\中海\\zip";
//        String fifter ="timeout";
        String path ="C:\\Users\\byf78\\Desktop\\日志分析\\稳定环境\\org";
//        String path ="C:\\Users\\byf78\\Desktop\\日志分析\\性能接口\\25";
//        String fifter ="initDomainResource entityList";
        String fifter ="[main";
//        String fifter ="track-selectListFrontNavMenu";
//        String fifter ="清理entityResource权限数据 by ids";

        getFifterZipFile(path,fifter);
        System.out.println("finish all ...");

    }

    public static void getFifterZipFile(String path,String fifter){
        List<File> pathAllFile = getPathAllFile(path, false);
        List<File> fifterFileByEndWith = getFifterFileByEndWith(pathAllFile, ".zip");
        List<File> files = checkZipListContainFifter(fifterFileByEndWith, fifter);
        for (File file : files) {
            System.out.println(file.getName());
        }
    }


    public static List<File> checkZipListContainFifter(List<File> files,String fifter){
        List<File> resList = new ArrayList<>();
        for (File file : files) {
            List<String> list = checkZipContainFifter(file, fifter);
//            for (String s : list) {
//                System.out.println(s);
//            }
            if(list!=null && !list.isEmpty()){
                appendOut(list,file);
                resList.add(file);
            }
        }
        return resList;
    }

    private static void appendOut(List<String> list,File file){
        String parent = file.getParent();
        String out = parent+"\\"+outFile;
        FileUtil.writeLines(list,out,"utf-8",true);
    }

    public static List<String> checkZipContainFifter(File zipFile,String fifter){
//        String absolutePath = zipFile.getParent();
//        String destDirPath = absolutePath+File.separator+"temp";
//        ZipUtil.unzip(zipFile.getAbsolutePath(), destDirPath);
        File unzip=null;
        try {
            unzip = ZipUtil.unzip(zipFile);
        }catch (Throwable e){
            System.out.println(e);
            return Lists.newArrayList();
        }

        if(unzip.isDirectory()){
            List<File> pathAllFile = getPathAllFile(unzip.getAbsolutePath(), true);
            List<String> stringList= new ArrayList<>();
            for (File file : pathAllFile) {
                List<String> list = FileUtil.readLines(file, "utf-8");
                List<String> collect = list.stream().filter(s -> s.contains(fifter)).collect(Collectors.toList());
                stringList.addAll(collect);
                file.delete();
            }
            unzip.delete();
            return stringList;
        }
        List<String> list = FileUtil.readLines(unzip, "utf-8");
        List<String> collect = list.stream().filter(s -> s.contains(fifter)).collect(Collectors.toList());
        unzip.delete();
        return collect;
    }


    public static List<File> getFifterFileByEndWith(List<File> files,String endWith){
        return files.stream().filter(f->f.getName().endsWith(endWith)).collect(Collectors.toList());
    }

    public static List<File> getPathAllFile(String path,boolean includChirld){
        if(includChirld){
            return FileUtil.loopFiles(path);
        }
//        File file1 = new File();
        List<String> list = FileUtil.listFileNames(path);
        List<File> fileList = new ArrayList<>();
        for (String s : list) {
            File file = new File(path+File.separator+s);
            if(file.isDirectory() || !file.exists()){
                continue;
            }
            fileList.add(file);
        }
        return fileList;
    }
}
